import { Fragment } from 'react';
import { Button, Tooltip } from 'antd';
import { DeleteOutlined } from '@ant-design/icons';
import pageStyles from '@/common/pageLayout.less';
import { SchemaManageAPI } from '@/components/SchemaComponent/SchemaManage/type';
import { EyeOutlined, PlusOutlined, CloudSyncOutlined, EditOutlined, BlockOutlined } from '@ant-design/icons';
import { ManageCommonAPI } from '@/pages/type';
import { tableMoreBtnConfig } from '@/utils/myutilConfig';
import { getPosIDs, getUserID, removeDateHmsStr, tooltipLength } from '@/utils';
import { isMomentAddFun, JZLZ_STATE, SHZ_STATE, XQBZ_STATE, ZRBGH_STATE } from '../common/Format';
import { BZZ_CHECKSTATE, ColumnsRenderFun, DictionaryRenderFun, SHTH_CHECKSTATE, formatDecimal } from '../../Acommon';

/**
 * @getSorter
 * @description 排序
 * @param prev
 * @param next
 * @returns
 */
const getSorter = (prev: Record<string, any>, next: Record<string, any>): number => (prev > next ? 1 : -1);

const Operation = (curRes: Record<string, any>, operateFuns: Record<string, any>, record: Record<string, any>) => {
  let operationArray: any = [];
  /** 一个按钮都没有时展示*/
  const Disable = (
    <Fragment key={record?.id + '--Disable'}>
      <Button style={{ color: '#000' }} className="manageBtntTxtStyle" type={'link'} size={'small'} disabled>
        --
      </Button>
    </Fragment>
  );

  /** 更多按钮配置 */
  const otherBtnConfig: ManageCommonAPI.TableMoreBtnConfigApi[] = [
    {
      icon: <EyeOutlined />,
      btnName: '查看',
      clickFnc: operateFuns,
      clickType: 'onView',
      isVisible: false,
    },
    {
      icon: <EyeOutlined />,
      btnName: '查看',
      clickFnc: operateFuns,
      clickType: 'onSync',
      isVisible: false,
    },
    {
      icon: <EditOutlined />,
      btnName: '编辑',
      clickFnc: operateFuns,
      clickType: 'onEdit',
      isVisible:
        isMomentAddFun() &&
        curRes?.['/purchaseNeedController/edit'] &&
        ((record?.state == XQBZ_STATE() && !record?.checkState && record?.createPositionID == getPosIDs() && record?.createrID == getUserID()) ||
          (record?.state == SHZ_STATE() &&
            record?.checkState == SHTH_CHECKSTATE() &&
            record?.applyerID == getUserID() &&
            record?.applyPositionID == getPosIDs()) ||
          (record?.state == JZLZ_STATE() &&
            record?.checkState == BZZ_CHECKSTATE() &&
            record?.applyerID == getUserID() &&
            record?.applyPositionID == getPosIDs())),
    },
    {
      icon: <CloudSyncOutlined />,
      btnName: '结果录入',
      clickFnc: operateFuns,
      clickType: 'onSync',
      isVisible:
        curRes?.['/purchaseNeedMaterialController/check'] &&
        record?.state == ZRBGH_STATE() &&
        record?.checkState == BZZ_CHECKSTATE() &&
        record?.applyerID == getUserID() &&
        record?.applyPositionID == getPosIDs(),
    },
    // {
    //   icon: <RollbackOutlined />,
    //   btnName: '撤销',
    //   clickFnc: operateFuns,
    //   clickType: 'onRevoke',
    //   isVisible:
    //     curRes['/purchaseNeedController/invalid'] &&
    //     record?.id &&
    //     record?.applyerID == getUserID() &&
    //     record?.applyPositionID == getPosIDs() &&
    //     (record?.state == BMSH_STATE() ||
    //       record?.state == KCBSH_STATE() ||
    //       record?.state == FGLDSH_STATE() ||
    //       record?.state == JZLZ_STATE() ||
    //       record?.state == ZRBGH_STATE()),
    // },
    {
      icon: <DeleteOutlined />,
      btnName: <span>删除</span>,
      clickFnc: operateFuns,
      clickType: 'onCancel',
      isVisible:
        record?.state == XQBZ_STATE() &&
        !record?.checkState &&
        curRes?.['/purchaseNeedController/delPurchaseNeed'] &&
        record?.createPositionID == getPosIDs() &&
        record?.createrID == getUserID(),
    },
  ];
  const ohterBtn = tableMoreBtnConfig(otherBtnConfig, record);
  operationArray = ohterBtn;
  operationArray = operationArray?.filter((itemBtn: any) => itemBtn);
  return operationArray && operationArray?.length > 0 ? operationArray : [Disable];
};

/**
 * @getNeedManageTableColumns
 * @description 计划管理数据表格信息展示和操作
 */
const getNeedManageTableColumns = (operation: any): Record<string, any>[] => {
  const { onView, onOA, needDictionary } = operation;
  const { supplyWayList, curRes } = needDictionary;

  return [
    {
      title: '计划类别',
      dataIndex: 'supplyWay',
      key: 'supplyWay',
      visible: true,
      fixed: 'left',
      sorter: (a: any, b: any) => getSorter(a?.supplyWay, b?.supplyWay),
      width: 160,
      render: (text: string) => {
        const value = supplyWayList.filter((item: Record<string, any>) => item.value == text)[0]?.name || '--';
        return <div className={'custom-td'}>{value}</div>;
      },
    },
    {
      title: '承办部门/团队', // 原【申请部门】
      dataIndex: 'applyDepartmentName',
      key: 'applyDepartmentName',
      visible: true,
      fixed: 'left',
      sorter: (a: any, b: any) => getSorter(a?.applyDepartmentID, b?.applyDepartmentID),
      width: 170,
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '计划标题',
      dataIndex: 'needName',
      key: 'needName',
      visible: true,
      width: 280,
      fixed: 'left',
      sorter: (a: any, b: any) => getSorter(a?.needName, b?.needName),
      render: (text: string, record: Record<string, any>) => {
        return (
          <a
            onClick={() => onView(record)}
            // onClick={() => onOA(record)}
            style={{ color: '100%' }}
          >
            <Tooltip title={tooltipLength(text, 50)}>
              <span className={pageStyles['over-flow-hidden']}>{text || '--'}</span>
            </Tooltip>
          </a>
        );
      },
    },
    {
      title: '预估总价(元)',
      dataIndex: 'expectSum',
      key: 'expectSum',
      visible: true,
      sorter: (a: any, b: any) => getSorter(a?.expectSum, b?.expectSum),
      width: 150,
      render: (text: number) => <span>{formatDecimal(text)}</span>,
    },
    {
      title: '计划状态',
      dataIndex: 'stateName',
      key: 'stateName',
      sorter: (a: any, b: any) => getSorter(a?.state, b?.state),
      visible: true,
      width: 150,
      render: (text: string) => DictionaryRenderFun({ text, isState: true }),
    },
    {
      title: '联系人',
      dataIndex: 'ownerName',
      key: 'ownerName',
      sorter: (a: any, b: any) => getSorter(a?.ownerName, b?.ownerName),
      visible: true,
      width: 150,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '联系电话',
      dataIndex: 'ownPhone',
      key: 'ownPhone',
      sorter: (a: any, b: any) => getSorter(a?.ownPhone, b?.ownPhone),
      visible: true,
      width: 150,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      key: 'createTime',
      sorter: (a: any, b: any) => getSorter(a?.createTime, b?.createTime),
      width: 190,
      visible: true,
      render: (text: string) => <span>{removeDateHmsStr(text)}</span>,
    },
    {
      title: '创建人',
      dataIndex: 'createrName',
      key: 'createrName',
      sorter: (a: any, b: any) => getSorter(a?.createrName, b?.createrName),
      width: 120,
      visible: false,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '创建人岗位',
      dataIndex: 'createPositionName',
      key: 'createPositionName',
      sorter: (a: any, b: any) => getSorter(a?.createPositionName, b?.createPositionName),
      width: 150,
      visible: false,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '创建人部门',
      dataIndex: 'createDepartmentName',
      key: 'createDepartmentName',
      sorter: (a: any, b: any) => getSorter(a?.createDepartmentName, b?.createDepartmentName),
      width: 150,
      visible: false,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '创建人公司',
      dataIndex: 'createCompanyName',
      key: 'createCompanyName',
      sorter: (a: any, b: any) => getSorter(a?.createCompanyName, b?.createCompanyName),
      width: 150,
      visible: false,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '经办时间',
      dataIndex: 'dutyTime',
      key: 'dutyTime',
      sorter: (a: any, b: any) => getSorter(a?.dutyTime, b?.dutyTime),
      width: 190,
      visible: false,
      render: (text: string) => <span>{removeDateHmsStr(text)}</span>,
    },
    {
      title: '经办人',
      dataIndex: 'dutyerName',
      key: 'dutyerName',
      visible: false,
      render: (text: string) => ColumnsRenderFun({ text }),
      width: 120,
      sorter: (a: any, b: any) => getSorter(a?.dutyerName, b?.dutyerName),
    },
    {
      title: '经办人岗位',
      dataIndex: 'dutyPositionName',
      key: 'dutyPositionName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => getSorter(a?.dutyPositionName, b?.dutyPositionName),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '经办人部门',
      dataIndex: 'dutyDepartmentName',
      key: 'dutyDepartmentName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => getSorter(a?.dutyDepartmentName, b?.dutyDepartmentName),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '经办人公司',
      dataIndex: 'dutyCompanyName',
      key: 'dutyCompanyName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => getSorter(a?.dutyCompanyName, b?.dutyCompanyName),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '修改时间',
      dataIndex: 'modifyTime',
      key: 'modifyTime',
      sorter: (a: any, b: any) => getSorter(a?.modifyTime, b?.modifyTime),
      width: 190,
      visible: false,
      render: (text: string) => <span>{removeDateHmsStr(text)}</span>,
    },
    {
      title: '修改人',
      dataIndex: 'modifyerName',
      key: 'modifyerName',
      visible: false,
      render: (text: string) => ColumnsRenderFun({ text }),
      width: 120,
      sorter: (a: any, b: any) => getSorter(a?.modifyerName, b?.modifyerName),
    },
    {
      title: '修改人岗位',
      dataIndex: 'modifyPositionName',
      key: 'modifyPositionName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => getSorter(a?.modifyPositionName, b?.modifyPositionName),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '修改人部门',
      dataIndex: 'modifyDepartmentName',
      key: 'modifyDepartmentName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => getSorter(a?.modifyDepartmentName, b?.modifyDepartmentName),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '修改人公司',
      dataIndex: 'modifyCompanyName',
      key: 'modifyCompanyName',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => getSorter(a?.modifyCompanyName, b?.modifyCompanyName),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '操作',
      dataIndex: 'operation',
      key: 'operation',
      width: 220,
      visible: true,
      unDrag: true,
      fixed: 'right',
      render: (_: string, record: Record<string, any>) => {
        return <div style={{ display: 'flex' }}>{Operation(curRes, operation, record)}</div>;
      },
    },
  ];
};

/**
 * @getNeedManageTableHeaderActions
 * @description 计划管理表格顶部自定义按钮
 * @returns SchemaManageAPI.IActionBtnList[]
 */
const getNeedManageTableHeaderActions = (operation: any): SchemaManageAPI.IActionBtnList[] => {
  const {
    onAdd,
    onCollect,
    needDictionary: { curRes },
  } = operation;

  return [
    {
      title: '添加',
      visible: !!curRes?.['/purchaseNeedController/add'] && isMomentAddFun(),
      type: 'primary',
      icon: <PlusOutlined />,
      onClick: () => {
        onAdd(0);
      },
    },
    {
      title: '汇总',
      visible: !!curRes?.['/purchaseNeedController/collect'],
      type: 'primary',
      icon: <BlockOutlined />,
      onClick: onCollect,
    },
    // {
    //   title: '实验平台',
    //   visible: !!curRes?.['/purchaseNeedController/add'],
    //   type: 'primary',
    //   icon: <PlusOutlined />,
    //   onClick: () => {
    //     onAdd(2);
    //   },
    // },
    // {
    //   title: '导出',
    //   visible: true,
    //   type: 'primary',
    //   icon: <ExportOutlined />,
    // },
  ];
};

export { getNeedManageTableColumns, getNeedManageTableHeaderActions };
